package com.basic.business.service.impl;

import com.basic.business.model.entity.SysDict;
import com.basic.business.model.dto.SysDictDto;
import com.basic.business.model.vo.SysDictVo;
import com.basic.business.mapper.SysDictMapper;
import com.basic.business.service.SysDictService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.*;

import java.util.*;

import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * 字典表(SysDict)表服务实现类
 *
 * @author huage
 * @since 2021-07-20 10:57:07
 */
@Service("sysDictService")
public class SysDictServiceImpl implements SysDictService {

    @Resource
    private SysDictMapper sysDictMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysDictVo findById(Long id) {
        SysDict sysDict = this.sysDictMapper.selectById(id);

        //将SysDict转成SysDictVo
        SysDictVo sysDictVo = null;
        if (sysDict != null) {
            sysDictVo = new SysDictVo();
            BeanUtils.copyProperties(sysDict, sysDictVo);
        }

        return sysDictVo;
    }

    /**
     * 查询多条数据
     *
     * @param sysDictDto 实例对象
     * @param pageNum    页数
     * @param pageSize   每页条数
     * @return 对象列表
     */
    @Override
    public PageInfo<SysDictVo> findPageList(SysDictDto sysDictDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //将SysDict转成SysDictVo
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictDto, sysDict);
        List<SysDict> sysDictList = this.sysDictMapper.selectList(sysDict);

        PageInfo<SysDictVo> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(new PageInfo<>(sysDictList), pageInfo);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param sysDictDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysDictVo add(SysDictDto sysDictDto) {
        //将SysDictDto转成SysDict
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictDto, sysDict);
        this.sysDictMapper.insert(sysDict);

        return this.findById(sysDict.getId());
    }

    /**
     * 修改数据
     *
     * @param sysDictDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysDictVo modify(SysDictDto sysDictDto) {
        //将SysDictDto转成SysDict
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictDto, sysDict);
        this.sysDictMapper.update(sysDict);

        return this.findById(sysDict.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean remove(Long id) {
        return this.sysDictMapper.deleteById(id) > 0;
    }
}

